from django.test import TestCase
from django.contrib.auth.models import User
from django.utils import timezone
from rest_framework.test import APIClient
from rest_framework import status
from datetime import datetime, timedelta
from .models import TodoList, TodoItem

class TodoTests(TestCase):
    def setUp(self):
        # 创建测试用户
        self.client = APIClient()
        self.user = User.objects.create_user(
            username='testuser',
            password='testpass123'
        )
        self.client.force_authenticate(user=self.user)
        
        # 创建测试数据
        self.todo_list = TodoList.objects.create(
            user=self.user,
            name='测试清单',
            description='测试清单描述'
        )
        
        self.todo_item_data = {
            'title': '测试待办事项',
            'description': '测试待办事项描述',
            'priority': 2,
            'status': 'pending',
            'due_date': (timezone.now() + timedelta(days=1)).isoformat(),
        }

    def test_create_todo_list(self):
        """测试创建待办清单"""
        response = self.client.post('/api/todo/lists/', {
            'name': '新清单',
            'description': '新清单描述',
            'is_default': False
        })
        self.assertEqual(response.status_code, status.HTTP_201_CREATED)
        self.assertEqual(TodoList.objects.count(), 2)

    def test_create_todo_item(self):
        """测试创建待办事项"""
        response = self.client.post(
            f'/api/todo/lists/{self.todo_list.id}/items/',
            self.todo_item_data
        )
        self.assertEqual(response.status_code, status.HTTP_201_CREATED)
        self.assertEqual(TodoItem.objects.count(), 1)

    def test_update_todo_item(self):
        """测试更新待办事项"""
        # 先创建一个待办事项
        item = TodoItem.objects.create(
            todo_list=self.todo_list,
            **self.todo_item_data
        )
        
        # 更新状态为已完成
        response = self.client.put(
            f'/api/todo/items/{item.id}/',
            {'status': 'completed'}
        )
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertEqual(
            TodoItem.objects.get(id=item.id).status,
            'completed'
        )

    def test_delete_todo_item(self):
        """测试删除待办事项"""
        # 先创建一个待办事项
        item = TodoItem.objects.create(
            todo_list=self.todo_list,
            **self.todo_item_data
        )
        
        response = self.client.delete(f'/api/todo/items/{item.id}/')
        self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT)
        self.assertEqual(TodoItem.objects.count(), 0)

    def test_get_todo_statistics(self):
        """测试获取待办事项统计"""
        # 创建一些测试数据
        TodoItem.objects.create(
            todo_list=self.todo_list,
            title='待完成任务',
            status='pending'
        )
        TodoItem.objects.create(
            todo_list=self.todo_list,
            title='已完成任务',
            status='completed'
        )
        
        response = self.client.get('/api/todo/statistics/')
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertIn('status', response.data)
        self.assertIn('priority', response.data)
        self.assertIn('overdue', response.data)
